package cn.gz.demo.ui;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import cn.gz.demo.R;
import cn.gz.demo.adapter.RecyclerViewTablayoutListAdapter;
import cn.gz.demo.databinding.RecyclerviewTablayoutBinding;
import cn.gz.demo.ui.base.BaseActivity;

/**
 * @author Liuyz
 * @date 2020/10/10 17:50
 */
public class RecyclerviewTablayoutActivity extends BaseActivity {
    private RecyclerviewTablayoutBinding binding;
    private List<String> list = new ArrayList<>();
    private RecyclerViewTablayoutListAdapter adapter;
    private LinearLayoutManager manager;
    private TopSmoothScroller smoothScroller;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview_tablayout);

        list.clear();
        binding.tablayout.removeAllTabs();
        for (int i = 0; i < 15; i++) {
            list.add("第" + i + "条数据");
            binding.tablayout.addTab(binding.tablayout.newTab().setText("第" + i + "个tab"));
        }

        smoothScroller = new TopSmoothScroller(context);

        manager = new LinearLayoutManager(context);
        binding.recyclerview.setLayoutManager(manager);
        adapter = new RecyclerViewTablayoutListAdapter(context, list);
        binding.recyclerview.setAdapter(adapter);

        binding.tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                smoothScroller.setTargetPosition(tab.getPosition());  //要滑动到的位置
                manager.startSmoothScroll(smoothScroller);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        binding.recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                binding.tablayout.setScrollPosition(manager.findFirstVisibleItemPosition(), 0, true);
            }
        });
    }

    class TopSmoothScroller extends LinearSmoothScroller {

        public TopSmoothScroller(Context context) {
            super(context);
        }

        @Override
        protected int getHorizontalSnapPreference() {
            return SNAP_TO_START;
        }

        @Override
        protected int getVerticalSnapPreference() {
            return SNAP_TO_START;
        }
    }
}
